#pragma once
#include <functional>
#include <signal.h>
#include <sigc++/sigc++.h>
#include <asio.hpp>

class SignalDispatcher {
public:
    explicit SignalDispatcher(asio::io_context& io)
        : io_(io), strand_(asio::make_strand(io)) {}

    template <typename Signal, typename Slot>
    void connect_async(Signal& signal, Slot&& slot) {
        signal.connect([this, slot = std::forward<Slot>(slot)](auto&&... args) {
            asio::post(strand_, [slot, args...]() mutable {
                slot(std::forward<decltype(args)>(args)...);
            });
        });
    }

    template <typename Func>
    void post(Func&& f) {
        asio::post(strand_, std::forward<Func>(f));
    }

private:
    asio::io_context& io_;
    asio::strand<asio::io_context::executor_type> strand_;
};

